package com.skp.plastory;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class ParsingJson {

	// Context ctx - temp
	public ArrayList<InfoStory> parsingStory(Context ctx, ArrayList<InfoStory> infoList) {

		try {
			BufferedInputStream buf = new BufferedInputStream(ctx.getResources().openRawResource(R.raw.my_story));

			BufferedReader bufReader = new BufferedReader(
					new InputStreamReader(buf, "utf-8"));

			String line = null;
			String page = "";

			while ((line = bufReader.readLine()) != null) {
				page += line;
			}

			JSONObject json = new JSONObject(page);

			Log.e("JSON 02", json.toString());

			JSONArray jArr = json.getJSONArray("my_story");


			for (int i = 0; i < jArr.length(); i++) {
				json = jArr.getJSONObject(i);

				String date = json.getString("date");
				String title = json.getString("title");
				String content = json.getString("content");
				String tag1 = json.getString("tag1");
				String tag2 = json.getString("tag2");
				String tag3 = json.getString("tag3");
				
				
				infoList.add(new InfoStory(title, content, date, tag1, tag2, tag3));
				//infoList.add(new InfoStory(title, date, tag1, tag2, tag3));
				
				/*
				et_page.append("date - " + date + "\n");
				et_page.append("title - " + title + "\n");
				et_page.append("tag1 - " + tag1 + "\n");
				et_page.append("tag2 - " + tag2 + "\n");
				et_page.append("tag3 - " + tag3 + "\n\n");
				*/
			}

		} catch (Exception e) {
			//et_page.append(e.getMessage());
		}

		return infoList;
	}

}
